﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_BackRecoveryDateCorrespondTab : DockContent
    {
        private DGBY.BLL.ConstDateCorrespondTab m_bllConstDateCorrespondTab = new DGBY.BLL.ConstDateCorrespondTab();
        private DGBY.BLL.DG_BackRecoveryDateCorrespondTab m_bllBackRecoveryDateCorrespondTab = new DGBY.BLL.DG_BackRecoveryDateCorrespondTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private bool m_bInsertFlag = false;
        private int m_nDelIndex = -1;

        public Frm_BackRecoveryDateCorrespondTab()
        {
            InitializeComponent();
        }

        private void Frm_BackRecoveryDateCorrespondTab_Load(object sender, EventArgs e)
        {

        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;
            InitColumnWidth();

            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            InitUltraDropDown();
        }

        private void InitUltraDropDown()
        {
            DataSet dsTemp = m_bllBGHDataTab.GetList("");

            this.ultraDropDownBGH.DataSource = dsTemp.Tables[0];
            this.ultraDropDownBGH.DisplayMember = "报关行简称";
            this.ultraDropDownBGH.ValueMember = "BGHDataTabID";
            this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns["报关行简称"].Hidden = false;

            dsTemp = m_bllConstDateCorrespondTab.GetList("");
     
            this.ultraDropDownFxt.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFxt.DisplayMember = "ConstDateCorrespondName";
            this.ultraDropDownFxt.ValueMember = "ConstDateCorrespondTabID";
            for (int i = 0; i < this.ultraDropDownFxt.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFxt.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFxt.DisplayLayout.Bands[0].Columns["ConstDateCorrespondName"].Hidden = false;
            this.ultraDropDownFxt.DisplayLayout.Bands[0].ColHeadersVisible = false;

            this.ultraDropDownLld.DataSource = dsTemp.Tables[0];
            this.ultraDropDownLld.DisplayMember = "ConstDateCorrespondName";
            this.ultraDropDownLld.ValueMember = "ConstDateCorrespondTabID";
            for (int i = 0; i < this.ultraDropDownLld.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownLld.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownLld.DisplayLayout.Bands[0].Columns["ConstDateCorrespondName"].Hidden = false;
            this.ultraDropDownLld.DisplayLayout.Bands[0].ColHeadersVisible = false;

            this.ultraDropDownRcd.DataSource = dsTemp.Tables[0];
            this.ultraDropDownRcd.DisplayMember = "ConstDateCorrespondName";
            this.ultraDropDownRcd.ValueMember = "ConstDateCorrespondTabID";
            for (int i = 0; i < this.ultraDropDownRcd.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownRcd.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownRcd.DisplayLayout.Bands[0].Columns["ConstDateCorrespondName"].Hidden = false;
            this.ultraDropDownRcd.DisplayLayout.Bands[0].ColHeadersVisible = false;

            this.ultraDropDownTyd.DataSource = dsTemp.Tables[0];
            this.ultraDropDownTyd.DisplayMember = "ConstDateCorrespondName";
            this.ultraDropDownTyd.ValueMember = "ConstDateCorrespondTabID";
            for (int i = 0; i < this.ultraDropDownTyd.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownTyd.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownTyd.DisplayLayout.Bands[0].Columns["ConstDateCorrespondName"].Hidden = false;
            this.ultraDropDownTyd.DisplayLayout.Bands[0].ColHeadersVisible = false;

            this.ultraDropDownTd.DataSource = dsTemp.Tables[0];
            this.ultraDropDownTd.DisplayMember = "ConstDateCorrespondName";
            this.ultraDropDownTd.ValueMember = "ConstDateCorrespondTabID";
            for (int i = 0; i < this.ultraDropDownTd.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownTd.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownTd.DisplayLayout.Bands[0].Columns["ConstDateCorrespondName"].Hidden = false;
            this.ultraDropDownTd.DisplayLayout.Bands[0].ColHeadersVisible = false;

            this.ultraDropDownWts.DataSource = dsTemp.Tables[0];
            this.ultraDropDownWts.DisplayMember = "ConstDateCorrespondName";
            this.ultraDropDownWts.ValueMember = "ConstDateCorrespondTabID";
            for (int i = 0; i < this.ultraDropDownWts.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownWts.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownWts.DisplayLayout.Bands[0].Columns["ConstDateCorrespondName"].Hidden = false;
            this.ultraDropDownWts.DisplayLayout.Bands[0].ColHeadersVisible = false;
        }

        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllBackRecoveryDateCorrespondTab.GetList(strWhere);
            ultraGrid1.DataSource = null;

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns["DG_BackRecoveryDateCorrespondTabID"].Hidden = true;

            ultraGrid1.DataSource = ds.Tables[0];
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["报关行"].ValueList = this.ultraDropDownBGH;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns["放行条"].ValueList = this.ultraDropDownFxt;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["六联单"].ValueList = this.ultraDropDownLld;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["入仓单"].ValueList = this.ultraDropDownRcd;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["托运单"].ValueList = this.ultraDropDownTyd;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["提单"].ValueList = this.ultraDropDownTd;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["委托书"].ValueList = this.ultraDropDownWts;
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
      
            InitNewAddRowValue(e.Row.Index);
        }

        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["报关行"].Value = 33;
            CurrentUGR.Cells["放行条"].Value = 0;
            CurrentUGR.Cells["六联单"].Value = 0;
            CurrentUGR.Cells["入仓单"].Value = 0;
            CurrentUGR.Cells["托运单"].Value = 0;
            CurrentUGR.Cells["提单"].Value = 0;
            CurrentUGR.Cells["委托书"].Value = 0;
           
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[1].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);
            }
        }

        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.DG_BackRecoveryDateCorrespondTab modelBackRecoveryDateCorrespondTab = new DGBY.Model.DG_BackRecoveryDateCorrespondTab();
                modelBackRecoveryDateCorrespondTab.BGHDataTabID = Convert.ToInt32(CurrentUGR.Cells["报关行"].Value.ToString());
                modelBackRecoveryDateCorrespondTab.fxtConstDateID = Convert.ToInt32(CurrentUGR.Cells["放行条"].Value.ToString());
                modelBackRecoveryDateCorrespondTab.lldConstDateID = Convert.ToInt32(CurrentUGR.Cells["六联单"].Value.ToString());
                modelBackRecoveryDateCorrespondTab.rcdConstDateID = Convert.ToInt32(CurrentUGR.Cells["入仓单"].Value.ToString());
                modelBackRecoveryDateCorrespondTab.tydConstDateID = Convert.ToInt32(CurrentUGR.Cells["托运单"].Value.ToString());
                modelBackRecoveryDateCorrespondTab.tdConstDateID = Convert.ToInt32(CurrentUGR.Cells["提单"].Value.ToString());
                modelBackRecoveryDateCorrespondTab.wtsConstDateID = Convert.ToInt32(CurrentUGR.Cells["委托书"].Value.ToString());

                int nID = m_bllBackRecoveryDateCorrespondTab.Add(modelBackRecoveryDateCorrespondTab);
;
                Frm_Main.Log2DB(this.Text, "增加", "");
                ShowAllRecord("DG_BackRecoveryDateCorrespondTabID = '" + nID.ToString() + "'");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关行"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["放行条"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["六联单"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["入仓单"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["托运单"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["提单"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["委托书"].Width = 120;
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;

                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.DG_BackRecoveryDateCorrespondTab modelBackRecoveryDateCorrespondTab = m_bllBackRecoveryDateCorrespondTab.GetModel(nSelectID);

                string strBGH = CurrentUGR.Cells["报关行"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelBackRecoveryDateCorrespondTab.BGHDataTabID = Convert.ToInt32(CurrentUGR.Cells["报关行"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBGHDataTab.GetList("BGHNameJX = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.BGHDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGHDataTabID"].ToString(), 10);
                    }
                }

                string strConstDateCorrespond = CurrentUGR.Cells["放行条"].Value.ToString();
                if (ClassInputValid.IsNum(strConstDateCorrespond))
                {
                    modelBackRecoveryDateCorrespondTab.fxtConstDateID = Convert.ToInt32(CurrentUGR.Cells["放行条"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllConstDateCorrespondTab.GetList("ConstDateCorrespondName = '" + strConstDateCorrespond + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.fxtConstDateID = Convert.ToInt32(ds.Tables[0].Rows[0]["ConstDateCorrespondTabID"].ToString(), 10);
                    }
                }

                strConstDateCorrespond = CurrentUGR.Cells["六联单"].Value.ToString();
                if (ClassInputValid.IsNum(strConstDateCorrespond))
                {
                    modelBackRecoveryDateCorrespondTab.lldConstDateID = Convert.ToInt32(CurrentUGR.Cells["六联单"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllConstDateCorrespondTab.GetList("ConstDateCorrespondName = '" + strConstDateCorrespond + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.lldConstDateID = Convert.ToInt32(ds.Tables[0].Rows[0]["ConstDateCorrespondTabID"].ToString(), 10);
                    }
                }

                strConstDateCorrespond = CurrentUGR.Cells["入仓单"].Value.ToString();
                if (ClassInputValid.IsNum(strConstDateCorrespond))
                {
                    modelBackRecoveryDateCorrespondTab.rcdConstDateID = Convert.ToInt32(CurrentUGR.Cells["入仓单"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllConstDateCorrespondTab.GetList("ConstDateCorrespondName = '" + strConstDateCorrespond + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.rcdConstDateID = Convert.ToInt32(ds.Tables[0].Rows[0]["ConstDateCorrespondTabID"].ToString(), 10);
                    }
                }

                strConstDateCorrespond = CurrentUGR.Cells["托运单"].Value.ToString();
                if (ClassInputValid.IsNum(strConstDateCorrespond))
                {
                    modelBackRecoveryDateCorrespondTab.tydConstDateID = Convert.ToInt32(CurrentUGR.Cells["托运单"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllConstDateCorrespondTab.GetList("ConstDateCorrespondName = '" + strConstDateCorrespond + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.tydConstDateID = Convert.ToInt32(ds.Tables[0].Rows[0]["ConstDateCorrespondTabID"].ToString(), 10);
                    }
                }

                strConstDateCorrespond = CurrentUGR.Cells["提单"].Value.ToString();
                if (ClassInputValid.IsNum(strConstDateCorrespond))
                {
                    modelBackRecoveryDateCorrespondTab.tdConstDateID = Convert.ToInt32(CurrentUGR.Cells["提单"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllConstDateCorrespondTab.GetList("ConstDateCorrespondName = '" + strConstDateCorrespond + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.tdConstDateID = Convert.ToInt32(ds.Tables[0].Rows[0]["ConstDateCorrespondTabID"].ToString(), 10);
                    }
                }

                strConstDateCorrespond = CurrentUGR.Cells["委托书"].Value.ToString();
                if (ClassInputValid.IsNum(strConstDateCorrespond))
                {
                    modelBackRecoveryDateCorrespondTab.wtsConstDateID = Convert.ToInt32(CurrentUGR.Cells["委托书"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllConstDateCorrespondTab.GetList("ConstDateCorrespondName = '" + strConstDateCorrespond + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBackRecoveryDateCorrespondTab.wtsConstDateID = Convert.ToInt32(ds.Tables[0].Rows[0]["ConstDateCorrespondTabID"].ToString(), 10);
                    }
                }


                m_bllBackRecoveryDateCorrespondTab.Update(modelBackRecoveryDateCorrespondTab);
                Frm_Main.Log2DB(this.Text, "修改", modelBackRecoveryDateCorrespondTab.DG_BackRecoveryDateCorrespondTabID.ToString());

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowCancelUpdate(object sender, CancelableRowEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_BeforeCellCancelUpdate(object sender, CancelableCellEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                DGBY.Model.DG_BackRecoveryDateCorrespondTab modelBackRecoveryDateCorrespondTab = m_bllBackRecoveryDateCorrespondTab.GetModel(nSelectID);

                if (modelBackRecoveryDateCorrespondTab == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllBackRecoveryDateCorrespondTab.Delete(nSelectID);
                        Frm_Main.Log2DB(this.Text, "删除", modelBackRecoveryDateCorrespondTab.DG_BackRecoveryDateCorrespondTabID.ToString());

                        ShowAllRecord("");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {

        }

        private void toolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR;
                    CurrentUGR = ultraGrid1.Rows[nIndex];
                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

    }
}
